package com.ch.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ch.gulimall.product.dao.BrandDao;
import com.ch.gulimall.product.dao.CategoryDao;
import com.ch.gulimall.product.entity.BrandEntity;
import com.ch.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ch.common.utils.PageUtils;
import com.ch.common.utils.Query;

import com.ch.gulimall.product.dao.CategoryBrandRelationDao;
import com.ch.gulimall.product.entity.CategoryBrandRelationEntity;
import com.ch.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        System.out.println("开始时间--------------->" + System.currentTimeMillis());
//        Thread t1 = new Thread(() -> {
//            //拿到品牌的id
//            Long brandId = categoryBrandRelation.getBrandId();
//            //通过品牌id拿到品牌的名字
//            BrandEntity brandEntity = brandDao.selectById(brandId);
//            categoryBrandRelation.setBrandName(brandEntity.getName());
//        });
//        Thread t2 = new Thread(() -> {
//            //拿到分类的id
//            Long catelogId = categoryBrandRelation.getCatelogId();
//            //通过分类的id拿到分类的名字
//            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
//            categoryBrandRelation.setCatelogName(categoryEntity.getName());
//        });
//        t1.start();
//        t2.start();
//        try {
//            t1.join();
//            t2.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        //拿到品牌的id
        Long brandId = categoryBrandRelation.getBrandId();
        //通过品牌id拿到品牌的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        //拿到分类的id
        Long catelogId = categoryBrandRelation.getCatelogId();
        //通过分类的id拿到分类的名字
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
        System.out.println("结束时间--------------->" + System.currentTimeMillis());
    }

    /**
     * 当品牌业务层发生品牌更新名字的时候 触发当前这个方法的调用 为了实时更新数据
     *
     * @param brandId 品牌id
     * @param name    品牌名字
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("brand_id", brandId);
        this.update(entity, wrapper);
    }

    /**
     * 当分类业务层发生更新分类名字的时候 触发当前这个方法的调用 为了实时更新数据
     *
     * @param catId 分类id
     * @param name  分类名字
     */
    @Override
    public void updateCatelog(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogId(catId);
        entity.setCatelogName(name);
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("catelog_id", catId);
        this.update(entity, wrapper);
    }

}
